package cn.stylefeng.guns.modular.vlt.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.util.CommUtils;
import cn.stylefeng.guns.modular.vlt.entity.Member;
import cn.stylefeng.guns.modular.vlt.mapper.MemberMapper;
import cn.stylefeng.guns.modular.vlt.model.params.MemberParam;
import cn.stylefeng.guns.modular.vlt.model.result.MemberResult;
import cn.stylefeng.guns.modular.vlt.service.MemberService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 参加活动人员表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-12-16
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Override
    public void add(MemberParam param) {
        Member entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MemberParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(MemberParam param) {
        Member oldEntity = getOldEntity(param);
        Member newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MemberResult findBySpec(MemberParam param) {
        return null;
    }

    @Override
    public List<MemberResult> findListBySpec(MemberParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(MemberParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<MemberResult> records = page.getRecords();
        records.forEach(ob -> {
            ob.setAge(CommUtils.IdNOToAge(ob.getIdCard()));
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(MemberParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Member getOldEntity(MemberParam param) {
        return this.getById(getKey(param));
    }

    private Member getEntity(MemberParam param) {
        Member entity = new Member();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
